﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IMDTO;
using IMBL;
using Ext.Net;

namespace IMPPFront.Management
{
    public partial class AFRMeta : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest || !IsPostBack)
            {
                //get data
                List<IMDTOAanvullendeFunctieRol> functieRollen = IMBLMetadata.GetAllAanvullendeFunctieRollen();
                List<IMDTORefCodeDetail> frFamilies = IMBLMetadata.GetAllAanvullendeFunctieRolFamilies();
                List<IMDTORefCodeDetail> frNiveaus = IMBLMetadata.GetAllFunctieRolNiveaus();

                //set data to grids
                FamilieStore.DataSource = frFamilies;
                FamilieStore.DataBind();
                NiveauStore.DataSource = frNiveaus;
                NiveauStore.DataBind();
                AFRStore.DataSource = functieRollen;
                AFRStore.DataBind();

                //set data to combo stores
                FamilieCombo.DataSource = frFamilies;
                FamilieCombo.DataBind();
                NiveauCombo.DataSource = frNiveaus;
                NiveauCombo.DataBind();
            }

        }

        public void FamilieStore_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            List<IMDTORefCodeDetail> frFamilies = IMBLMetadata.GetAllAanvullendeFunctieRolFamilies();
            FamilieStore.DataSource = frFamilies;
            FamilieStore.DataBind();
            FamilieCombo.DataSource = frFamilies;
            FamilieCombo.DataBind();

        }

        public void NiveauStore_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            List<IMDTORefCodeDetail> frNiveaus = IMBLMetadata.GetAllFunctieRolNiveaus();
            NiveauStore.DataSource = frNiveaus;
            NiveauStore.DataBind();
            NiveauCombo.DataSource = frNiveaus;
            NiveauCombo.DataBind();
        }

        public void HandleRefCodeChanges(object sender, BeforeStoreChangedEventArgs e)
        {
            ChangeRecords<IMDTORefCodeDetail> refcodes = e.DataHandler.ObjectData<IMDTORefCodeDetail>();
            Store senderStore = (Store)sender;

            foreach (IMDTORefCodeDetail created in refcodes.Created)
            {
                int tempId = created.idRefCodeDetail;
                int newId = IMBLMetadata.AddRefCodeDetail(created);
                if (senderStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(tempId.ToString(), newId.ToString());
                }
                else
                {
                    senderStore.UpdateRecordId(tempId, newId);
                }

            }

            foreach (IMDTORefCodeDetail deleted in refcodes.Deleted)
            {
                IMBLMetadata.DeleteRefCodeDetail(deleted.idRefCodeDetail);

                if (senderStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(deleted.idRefCodeDetail.ToString());
                }
            }

            foreach (IMDTORefCodeDetail updated in refcodes.Updated)
            {
                IMBLMetadata.UpdateRefCodeDetail(updated);

                if (senderStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(updated.idRefCodeDetail.ToString());
                }
            }
            e.Cancel = true;
        }

        protected void HandleChanges(object sender, BeforeStoreChangedEventArgs e)
        {
            ChangeRecords<IMDTOAanvullendeFunctieRol> functierollen = e.DataHandler.ObjectData<IMDTOAanvullendeFunctieRol>();

            foreach (IMDTOAanvullendeFunctieRol created in functierollen.Created)
            {
                int tempId = created.idAanvullendeFunctieRol;
                int newId = IMBLMetadata.AddAanvullendeFunctieRol(created);
                if (AFRStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(tempId.ToString(), newId.ToString());
                }
                else
                {
                    AFRStore.UpdateRecordId(tempId, newId);
                }

            }

            foreach (IMDTOAanvullendeFunctieRol deleted in functierollen.Deleted)
            {
                IMBLMetadata.DeleteAanvullendeFunctieRol(deleted.idAanvullendeFunctieRol);

                if (AFRStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(deleted.idAanvullendeFunctieRol.ToString());
                }
            }

            foreach (IMDTOAanvullendeFunctieRol updated in functierollen.Updated)
            {
                IMBLMetadata.UpdateAanvullendeFunctieRol(updated);

                if (AFRStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(updated.idAanvullendeFunctieRol.ToString());
                }
            }
            e.Cancel = true;
        }
    }
}